package com.cloudsigns.factorymanager2.produce.activity.produce_team;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.cloudsigns.factorymanager2.BaseActivity;
import com.cloudsigns.factorymanager2.R;
import com.cloudsigns.factorymanager2.cusenum.CommonEnum;
import com.cloudsigns.factorymanager2.engineering.adapter.GridAdapter;
import com.cloudsigns.factorymanager2.produce.activity.AvatarImageShowerActivity;
import com.cloudsigns.factorymanager2.produce.bean.WorkPartBean;
import com.cloudsigns.factorymanager2.produce.bean.WorkPartDailyFeedbackBean;
import com.cloudsigns.factorymanager2.produce.bean.WorkPartOrderBean;
import com.cloudsigns.factorymanager2.produce.iview.produce_team.IDailyFeedbackDeleteView;
import com.cloudsigns.factorymanager2.produce.iview.produce_team.IWorkPartFeedbackDetailView;
import com.cloudsigns.factorymanager2.produce.presenter.produce_team.DailyFeedbackDeletePresenter;
import com.cloudsigns.factorymanager2.produce.presenter.produce_team.WorkPartFeedbackDetailPresenter;
import com.cloudsigns.factorymanager2.produce.utils.ProduceForTeamEvent;
import com.cloudsigns.factorymanager2.project.utils.ConstantUtil;
import com.cloudsigns.factorymanager2.project.utils.DateUtil;

import java.util.ArrayList;
import java.util.List;

import de.greenrobot.event.EventBus;

public class ProduceTeamDailyFeedbackDetailActivity extends BaseActivity implements IWorkPartFeedbackDetailView, IDailyFeedbackDeleteView {
    private RelativeLayout rl_back;
    private TextView tv_title;
    private Button btn_delete;
    private LinearLayout ll_bottom;
    private TextView tv_feedback_type, tv_submit_person, tv_submit_time, tv_confirm_person, tv_confirm_time;
    private LinearLayout ll_confirm_person, ll_confirm_time;
    private View v_1;

    private LinearLayout ll_normal_feedback, ll_plan_feedback, ll_machin_feedback, ll_apply_adjust, ll_apply_return, ll_apply_scrap;

    /*正常反馈*/
    private TextView tv_normal_plan_amount, tv_normal_daily_output, tv_normal_feedback_content;
    private GridView normal_gridView;

    /*计划异常*/
    private TextView tv_plan_unusual_classify, tv_plan_amount, tv_plan_daily_output, tv_plan_unfinished_reason, tv_plan_advice;

    /*加工异常*/
    private TextView tv_machin_unusual_classify, tv_machin_unusual_reason, tv_machin_day_number, tv_machin_advice;
    private GridView machin_gridView;

    /*申请调整*/
    private TextView tv_apply_adjust_day, tv_apply_adjust_reason;

    /*申请返工*/
    private TextView tv_apply_return_num, tv_apply_return_reason;
    private GridView return_gridView;

    /*申请报废*/
    private TextView tv_apply_scrap_num, tv_apply_scrap_reason;
    private GridView scrap_gridView;

    private GridAdapter gridAdapter;

    private WorkPartFeedbackDetailPresenter workPartFeedbackDetailPresenter;

    private WorkPartBean workPartBean;
    private WorkPartOrderBean workPartOrderBean;
    private WorkPartDailyFeedbackBean workPartDailyFeedbackBean;

    private int count = 0;

    private DailyFeedbackDeletePresenter dailyFeedbackDeletePresenter;

    @Override
    protected void onBaseCreate(Bundle savedInstanceState) {
        setContentView(R.layout.layout_produce_team_daily_feedback_detail);
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        rl_back = findViewById(R.id.rl_back);
        tv_title = findViewById(R.id.tv_title);
        tv_feedback_type = findViewById(R.id.tv_feedback_type);
        tv_submit_person = findViewById(R.id.tv_submit_person);
        tv_submit_time = findViewById(R.id.tv_submit_time);
        tv_confirm_person = findViewById(R.id.tv_confirm_person);
        tv_confirm_time = findViewById(R.id.tv_confirm_time);
        ll_confirm_person = findViewById(R.id.ll_confirm_person);
        ll_confirm_time = findViewById(R.id.ll_confirm_time);
        v_1 = findViewById(R.id.v_1);
        btn_delete = findViewById(R.id.btn_delete);
        ll_bottom = findViewById(R.id.ll_bottom);
        ll_normal_feedback = findViewById(R.id.ll_normal_feedback);
        ll_plan_feedback = findViewById(R.id.ll_plan_feedback);
        ll_machin_feedback = findViewById(R.id.ll_machin_feedback);
        ll_apply_adjust = findViewById(R.id.ll_apply_adjust);
        ll_apply_return = findViewById(R.id.ll_apply_return);
        ll_apply_scrap = findViewById(R.id.ll_apply_scrap);
        tv_normal_plan_amount = findViewById(R.id.tv_normal_plan_amount);
        tv_normal_daily_output = findViewById(R.id.tv_normal_daily_output);
        tv_normal_feedback_content = findViewById(R.id.tv_normal_feedback_content);
        normal_gridView = findViewById(R.id.normal_gridView);
        tv_plan_unusual_classify = findViewById(R.id.tv_plan_unusual_classify);
        tv_plan_amount = findViewById(R.id.tv_plan_amount);
        tv_plan_daily_output = findViewById(R.id.tv_plan_daily_output);
        tv_plan_unfinished_reason = findViewById(R.id.tv_plan_unfinished_reason);
        tv_plan_advice = findViewById(R.id.tv_plan_advice);
        tv_machin_unusual_classify = findViewById(R.id.tv_machin_unusual_classify);
        tv_machin_unusual_reason = findViewById(R.id.tv_machin_unusual_reason);
        tv_machin_day_number = findViewById(R.id.tv_machin_day_number);
        tv_machin_advice = findViewById(R.id.tv_machin_advice);
        machin_gridView = findViewById(R.id.machin_gridView);
        tv_apply_adjust_day = findViewById(R.id.tv_apply_adjust_day);
        tv_apply_adjust_reason = findViewById(R.id.tv_apply_adjust_reason);
        tv_apply_return_num = findViewById(R.id.tv_apply_return_num);
        tv_apply_return_reason = findViewById(R.id.tv_apply_return_reason);
        return_gridView = findViewById(R.id.return_gridView);
        tv_apply_scrap_num = findViewById(R.id.tv_apply_scrap_num);
        tv_apply_scrap_reason = findViewById(R.id.tv_apply_scrap_reason);
        scrap_gridView = findViewById(R.id.scrap_gridView);
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        tv_title.setText(getResources().getString(R.string.produce_feedback));

        Intent intent = getIntent();
        if (null != intent) {
            workPartBean = (WorkPartBean) intent.getSerializableExtra(ConstantUtil.KEY_1);
            workPartOrderBean = (WorkPartOrderBean) intent.getSerializableExtra(ConstantUtil.KEY_2);
            workPartDailyFeedbackBean = (WorkPartDailyFeedbackBean) intent.getSerializableExtra(ConstantUtil.KEY_3);
            count = intent.getIntExtra(ConstantUtil.KEY_4, 0);
        }

        workPartFeedbackDetailPresenter = new WorkPartFeedbackDetailPresenter(this);
//        workPartFeedbackDetailPresenter.workPartFeedbackDetail();

        dailyFeedbackDeletePresenter = new DailyFeedbackDeletePresenter(this);

        CommonEnum.WorkPartType type = workPartDailyFeedbackBean.getType();
        if (null != type) {
            if (type == CommonEnum.WorkPartType.NORMAL) {
                tv_feedback_type.setText(getResources().getString(R.string.normal_feedback));

                ll_normal_feedback.setVisibility(View.VISIBLE);
                ll_plan_feedback.setVisibility(View.GONE);
                ll_machin_feedback.setVisibility(View.GONE);
                ll_apply_adjust.setVisibility(View.GONE);
                ll_apply_return.setVisibility(View.GONE);
                ll_apply_scrap.setVisibility(View.GONE);

                tv_normal_plan_amount.setText(String.valueOf(count));
                tv_normal_daily_output.setText(String.valueOf(workPartDailyFeedbackBean.getCompleteCount()));
                tv_normal_feedback_content.setText(workPartDailyFeedbackBean.getFeedbackInfo());

                gridAdapter = new GridAdapter(this);
                gridAdapter.setData(workPartDailyFeedbackBean.getImgUrlList());
                normal_gridView.setAdapter(gridAdapter);


                if (null != workPartDailyFeedbackBean.getHandlerAccountId() && !TextUtils.isEmpty(workPartDailyFeedbackBean.getHandlerAccountId()) && !workPartDailyFeedbackBean.getHandlerAccountId().equals("null")) {
                    ll_bottom.setVisibility(View.GONE);
                    v_1.setVisibility(View.VISIBLE);
                    ll_confirm_person.setVisibility(View.VISIBLE);
                    ll_confirm_time.setVisibility(View.VISIBLE);
                } else {
                    ll_bottom.setVisibility(View.VISIBLE);
                    v_1.setVisibility(View.GONE);
                    ll_confirm_person.setVisibility(View.GONE);
                    ll_confirm_time.setVisibility(View.GONE);
                }

            } else if (type == CommonEnum.WorkPartType.UNUSUAL) {
                tv_feedback_type.setText(getResources().getString(R.string.unusual_feedback));

                CommonEnum.WorkPartSubType subType = workPartDailyFeedbackBean.getSubType();
                if (null != subType) {
                    if (subType == CommonEnum.WorkPartSubType.PLAN_UNUSUAL) {
                        tv_plan_unusual_classify.setText(getResources().getString(R.string.plan_unusual));

                        ll_normal_feedback.setVisibility(View.GONE);
                        ll_plan_feedback.setVisibility(View.VISIBLE);
                        ll_machin_feedback.setVisibility(View.GONE);
                        ll_apply_adjust.setVisibility(View.GONE);
                        ll_apply_return.setVisibility(View.GONE);
                        ll_apply_scrap.setVisibility(View.GONE);

                        tv_plan_amount.setText(String.valueOf(count));
                        tv_plan_daily_output.setText(String.valueOf(workPartDailyFeedbackBean.getCompleteCount()));
                        tv_plan_unfinished_reason.setText(workPartDailyFeedbackBean.getUnusualInfo());
                        tv_plan_advice.setText(workPartDailyFeedbackBean.getUnusualAdvice());

                        CommonEnum.FeedbackStatus feedbackStatus = workPartDailyFeedbackBean.getFeedbackStatus();
                        if (null != feedbackStatus) {
                            if (feedbackStatus == CommonEnum.FeedbackStatus.WAIT_CONFIRM || feedbackStatus == CommonEnum.FeedbackStatus.CONFIRM_FINISH || feedbackStatus == CommonEnum.FeedbackStatus.WAIT_REPLY_CONFIRM) {
                                ll_bottom.setVisibility(View.VISIBLE);
                                v_1.setVisibility(View.GONE);
                                ll_confirm_person.setVisibility(View.GONE);
                                ll_confirm_time.setVisibility(View.GONE);

                            } else if (feedbackStatus == CommonEnum.FeedbackStatus.REPLY_CONFIRM_FINISH) {
                                ll_bottom.setVisibility(View.GONE);
                                v_1.setVisibility(View.VISIBLE);
                                ll_confirm_person.setVisibility(View.VISIBLE);
                                ll_confirm_time.setVisibility(View.VISIBLE);
                            }
                        }

                    } else if (subType == CommonEnum.WorkPartSubType.PRODUCE_UNUSUAL) {
                        tv_machin_unusual_classify.setText(getResources().getString(R.string.machin_unusual));

                        ll_normal_feedback.setVisibility(View.GONE);
                        ll_plan_feedback.setVisibility(View.GONE);
                        ll_machin_feedback.setVisibility(View.VISIBLE);
                        ll_apply_adjust.setVisibility(View.GONE);
                        ll_apply_return.setVisibility(View.GONE);
                        ll_apply_scrap.setVisibility(View.GONE);

                        tv_machin_unusual_reason.setText(workPartDailyFeedbackBean.getUnusualInfo());
                        tv_machin_day_number.setText(workPartDailyFeedbackBean.getUnusualDays() + "天");
                        tv_machin_advice.setText(workPartDailyFeedbackBean.getUnusualAdvice());

                        gridAdapter = new GridAdapter(this);
                        gridAdapter.setData(workPartDailyFeedbackBean.getImgUrlList());
                        machin_gridView.setAdapter(gridAdapter);


                        CommonEnum.FeedbackStatus feedbackStatus = workPartDailyFeedbackBean.getFeedbackStatus();
                        if (null != feedbackStatus) {
                            if (feedbackStatus == CommonEnum.FeedbackStatus.WAIT_CONFIRM || feedbackStatus == CommonEnum.FeedbackStatus.CONFIRM_FINISH || feedbackStatus == CommonEnum.FeedbackStatus.WAIT_REPLY_CONFIRM) {
                                ll_bottom.setVisibility(View.VISIBLE);
                                v_1.setVisibility(View.GONE);
                                ll_confirm_person.setVisibility(View.GONE);
                                ll_confirm_time.setVisibility(View.GONE);

                            } else if (feedbackStatus == CommonEnum.FeedbackStatus.REPLY_CONFIRM_FINISH) {
                                ll_bottom.setVisibility(View.GONE);
                                v_1.setVisibility(View.VISIBLE);
                                ll_confirm_person.setVisibility(View.VISIBLE);
                                ll_confirm_time.setVisibility(View.VISIBLE);
                            }
                        }
                    }
                }
            } else if (type == CommonEnum.WorkPartType.APPLY_ADJUST) {
                tv_feedback_type.setText(getResources().getString(R.string.application_adjustment));

                ll_normal_feedback.setVisibility(View.GONE);
                ll_plan_feedback.setVisibility(View.GONE);
                ll_machin_feedback.setVisibility(View.GONE);
                ll_apply_adjust.setVisibility(View.VISIBLE);
                ll_apply_return.setVisibility(View.GONE);
                ll_apply_scrap.setVisibility(View.GONE);

                tv_apply_adjust_day.setText(workPartDailyFeedbackBean.getApplyDays() + "天");
                tv_apply_adjust_reason.setText(workPartDailyFeedbackBean.getFeedbackInfo());

                if (null != workPartDailyFeedbackBean.getHandlerAccountId() && !TextUtils.isEmpty(workPartDailyFeedbackBean.getHandlerAccountId()) && !workPartDailyFeedbackBean.getHandlerAccountId().equals("null")) {
                    ll_bottom.setVisibility(View.GONE);
                    v_1.setVisibility(View.VISIBLE);
                    ll_confirm_person.setVisibility(View.VISIBLE);
                    ll_confirm_time.setVisibility(View.VISIBLE);
                } else {
                    ll_bottom.setVisibility(View.VISIBLE);
                    v_1.setVisibility(View.GONE);
                    ll_confirm_person.setVisibility(View.GONE);
                    ll_confirm_time.setVisibility(View.GONE);
                }

            } else if (type == CommonEnum.WorkPartType.APPLY_RETURN) {
                tv_feedback_type.setText(getResources().getString(R.string.application_rework));

                ll_normal_feedback.setVisibility(View.GONE);
                ll_plan_feedback.setVisibility(View.GONE);
                ll_machin_feedback.setVisibility(View.GONE);
                ll_apply_adjust.setVisibility(View.GONE);
                ll_apply_return.setVisibility(View.VISIBLE);
                ll_apply_scrap.setVisibility(View.GONE);

                tv_apply_return_num.setText(String.valueOf(workPartDailyFeedbackBean.getApplyAmount()));
                tv_apply_return_reason.setText(workPartDailyFeedbackBean.getFeedbackInfo());

                gridAdapter = new GridAdapter(this);
                gridAdapter.setData(workPartDailyFeedbackBean.getImgUrlList());
                return_gridView.setAdapter(gridAdapter);

                if (null != workPartDailyFeedbackBean.getHandlerAccountId() && !TextUtils.isEmpty(workPartDailyFeedbackBean.getHandlerAccountId()) && !workPartDailyFeedbackBean.getHandlerAccountId().equals("null")) {
                    ll_bottom.setVisibility(View.GONE);
                    v_1.setVisibility(View.VISIBLE);
                    ll_confirm_person.setVisibility(View.VISIBLE);
                    ll_confirm_time.setVisibility(View.VISIBLE);
                } else {
                    ll_bottom.setVisibility(View.VISIBLE);
                    v_1.setVisibility(View.GONE);
                    ll_confirm_person.setVisibility(View.GONE);
                    ll_confirm_time.setVisibility(View.GONE);
                }

            } else if (type == CommonEnum.WorkPartType.APPLY_SCRAP) {
                tv_feedback_type.setText(getResources().getString(R.string.application_scrap));

                ll_normal_feedback.setVisibility(View.GONE);
                ll_plan_feedback.setVisibility(View.GONE);
                ll_machin_feedback.setVisibility(View.GONE);
                ll_apply_adjust.setVisibility(View.GONE);
                ll_apply_return.setVisibility(View.GONE);
                ll_apply_scrap.setVisibility(View.VISIBLE);

                tv_apply_scrap_num.setText(String.valueOf(workPartDailyFeedbackBean.getApplyAmount()));
                tv_apply_scrap_reason.setText(workPartDailyFeedbackBean.getFeedbackInfo());

                gridAdapter = new GridAdapter(this);
                gridAdapter.setData(workPartDailyFeedbackBean.getImgUrlList());
                scrap_gridView.setAdapter(gridAdapter);

                if (null != workPartDailyFeedbackBean.getHandlerAccountId() && !TextUtils.isEmpty(workPartDailyFeedbackBean.getHandlerAccountId()) && !workPartDailyFeedbackBean.getHandlerAccountId().equals("null")) {
                    ll_bottom.setVisibility(View.GONE);
                    v_1.setVisibility(View.VISIBLE);
                    ll_confirm_person.setVisibility(View.VISIBLE);
                    ll_confirm_time.setVisibility(View.VISIBLE);
                } else {
                    ll_bottom.setVisibility(View.VISIBLE);
                    v_1.setVisibility(View.GONE);
                    ll_confirm_person.setVisibility(View.GONE);
                    ll_confirm_time.setVisibility(View.GONE);
                }
            }
        }

        tv_submit_person.setText(workPartDailyFeedbackBean.getAccountName());
        tv_submit_time.setText(DateUtil.getYyyy_Mm_Dd(workPartDailyFeedbackBean.getFeedbackDate()));
        tv_confirm_person.setText(workPartDailyFeedbackBean.getHandlerAccountName());
        tv_confirm_time.setText(DateUtil.getYyyy_Mm_Dd(workPartDailyFeedbackBean.getHandleDate()));
    }

    @Override
    public void setListener() {
        rl_back.setOnClickListener(this);
        btn_delete.setOnClickListener(this);

        normal_gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long l) {
                Intent intent = new Intent(ProduceTeamDailyFeedbackDetailActivity.this, AvatarImageShowerActivity.class);
                intent.putExtra("imageUrl", workPartDailyFeedbackBean.getImgUrlList().get(position));
                startActivity(intent);
            }
        });

        machin_gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long l) {
                Intent intent = new Intent(ProduceTeamDailyFeedbackDetailActivity.this, AvatarImageShowerActivity.class);
                intent.putExtra("imageUrl", workPartDailyFeedbackBean.getImgUrlList().get(position));
                startActivity(intent);
            }
        });

        return_gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long l) {
                Intent intent = new Intent(ProduceTeamDailyFeedbackDetailActivity.this, AvatarImageShowerActivity.class);
                intent.putExtra("imageUrl", workPartDailyFeedbackBean.getImgUrlList().get(position));
                startActivity(intent);
            }
        });

        scrap_gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long l) {
                Intent intent = new Intent(ProduceTeamDailyFeedbackDetailActivity.this, AvatarImageShowerActivity.class);
                intent.putExtra("imageUrl", workPartDailyFeedbackBean.getImgUrlList().get(position));
                startActivity(intent);
            }
        });
    }

    @Override
    public void widgetClick(View view) {
        switch (view.getId()) {
            case R.id.rl_back:
                finish();
                break;
            // 删除
            case R.id.btn_delete:
                dailyFeedbackDeletePresenter.dailyFeedbackDelete();
                break;
        }
    }

    @Override
    public String getOrderId() {
        return workPartBean.getOrderId();
    }

    @Override
    public String getProductId() {
        return workPartBean.getProductId();
    }

    @Override
    public String getWorkPartFeedbackId() {
        return workPartDailyFeedbackBean.getWorkPartFeedbackId();
    }

    @Override
    public String getWorkPartId() {
        return workPartBean.getWorkPartId();
    }

    @Override
    public String getWorkPartOrderId() {
        return workPartOrderBean.getWorkPartOrderId();
    }

    @Override
    public void getDailyFeedbackDeleteSuccessed() {
        showToast(getResources().getString(R.string.delete_success));
        EventBus.getDefault().post(new ProduceForTeamEvent());
        finish();
    }

    @Override
    public void getDailyFeedbackDeleteFailed(final String desc) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                showToast(desc);
            }
        });
    }

    @Override
    public void getWorkPartFeedbackDetailSuccessed(WorkPartDailyFeedbackBean workPartDailyFeedbackBean) {

    }

    @Override
    public void getWorkPartFeedbackDetailFailed(final String desc) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                showToast(desc);
            }
        });
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }
}
